package com.uniagus.Controller;

import com.uniagus.Modelo.Jornada;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSessionContext;
import org.apache.coyote.Adapter;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Sefer Sephirott
 */
public class StartSesionServlet extends HttpServlet 
{
    private static final long serialVersionUID = 1L;
    
    private String id;
    private String nombreJor;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
    {
        
        processRequest(request, response);
        
        id = request.getParameter("idAlum");
        
        HttpSession session = request.getSession(true);
        
        String x = validar(id);
        
        if(x.equals("Nocturna")){
            session.setAttribute("id", id);
            response.sendRedirect("http://localhost:8084/ModeloAplicativoWeb/votar.jsp");
        }
        else{
            response.sendRedirect("http://localhost:8084/ModeloAplicativoWeb/NoAutorizado.jsp");
        }
    }
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public String validar(String ID){
        
        Configuration config = new Configuration().configure();
        SessionFactory factory = config.buildSessionFactory();
        
        Session sesion = factory.openSession();
        
        try{
            Jornada jor = (Jornada)sesion.get(Jornada.class, Integer.parseInt(ID));
            
            return jor.getNombre();
 
        }
        catch(Exception e){
            
            System.out.println(e);
            return null;
        }
    }
  
}
